# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=qt5

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}-git"
_ver_base=5.3.0
pkgver=5.3.0.890.e198c12
pkgrel=1
arch=('any')
url='http://qt-project.org/'
install=qt5.install
license=('GPL3' 'LGPL' 'FDL' 'custom')
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
depends=("${MINGW_PACKAGE_PREFIX}-gcc"
      "${MINGW_PACKAGE_PREFIX}-icu"
      "${MINGW_PACKAGE_PREFIX}-openssl"
      "${MINGW_PACKAGE_PREFIX}-libxml2"
      "${MINGW_PACKAGE_PREFIX}-libxslt"
      "${MINGW_PACKAGE_PREFIX}-fontconfig"
      "${MINGW_PACKAGE_PREFIX}-qtbinpatcher"
      "${MINGW_PACKAGE_PREFIX}-freetype"
      "${MINGW_PACKAGE_PREFIX}-libpng"
      "${MINGW_PACKAGE_PREFIX}-zlib"
      "${MINGW_PACKAGE_PREFIX}-libjpeg"
      "${MINGW_PACKAGE_PREFIX}-pcre"
      "${MINGW_PACKAGE_PREFIX}-sqlite3"
      "${MINGW_PACKAGE_PREFIX}-libwebp")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
      "${MINGW_PACKAGE_PREFIX}-postgresql"
      "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
      "${MINGW_PACKAGE_PREFIX}-libfbclient"
      "${MINGW_PACKAGE_PREFIX}-python2"
      "${MINGW_PACKAGE_PREFIX}-ruby"
      "perl"
      "gperf"
      "m4"
      "flex"
      "${MINGW_PACKAGE_PREFIX}-make")
groups=("${MINGW_PACKAGE_PREFIX}-qt" "${MINGW_PACKAGE_PREFIX}-qt5")
options=('!strip' 'staticlibs')
QT_GIT_BRANCH=stable
qt_modules=(
    qt5
    qtactiveqt
    qtandroidextras
    qtbase
    qtconnectivity
    qtdeclarative
    qtdoc
    qtgraphicaleffects
    qtimageformats
    qtlocation
    qtmultimedia
    qtquick1
    qtquickcontrols
    qtscript
    qtsensors
    qtserialport
    qtsvg
    qttools
    qttranslations
    qtwebkit
    qtwebkit-examples
    qtwinextras
    qtx11extras
    qtxmlpatterns
    )
source=($( \
      for _mod in ${qt_modules[@]}; do \
        echo "${_mod}"::"git://gitorious.org/qt/${_mod}.git#branch=$QT_GIT_BRANCH"; \
      done \
    )
    qt-5.0.0-oracle-driver-prompt.patch
    qt-5.0.0-use-fbclient-instead-of-gds32.patch
    qt-5.1.0-win32-g++-mkspec-optimization.patch
    qt-5.1.x-fix-configure-tests.patch
    qt-5.1.x-syncqt-fix.patch
    qt-5.1.x-win_flex-replace.patch
    qt-5.2.1-static-qmake-conf.patch
    qt-5.2.1-fix-qtAddToolEnv-under-MSYS2-with-mingw32-make.patch)

pkgver() {
  cd "$srcdir/$_realname"
  printf "%s.%s.%s" "$_ver_base" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${srcdir}
  for curmod in ${qt_modules[@]}; do
    if [[ $curmod != ${_realname} ]]; then
      mv -f $curmod ${_realname}
    fi
  done
  
  cd ${_realname}

  plain ""
  patch -p1 -i ${srcdir}/qt-5.0.0-oracle-driver-prompt.patch
  patch -p1 -i ${srcdir}/qt-5.0.0-use-fbclient-instead-of-gds32.patch
  patch -p1 -i ${srcdir}/qt-5.1.0-win32-g++-mkspec-optimization.patch
  #patch -p1 -i ${srcdir}/qt-5.1.x-fix-configure-tests.patch
  patch -p1 -i ${srcdir}/qt-5.1.x-syncqt-fix.patch
  patch -p1 -i ${srcdir}/qt-5.1.x-win_flex-replace.patch

  local _optim=
  case ${MINGW_CHOST} in
  i686*)
    _optim="-march=i686 -mtune=core2"
  ;;
  x86_64*)
    _optim="-march=nocona -mtune=core2"
  ;;
  esac

  sed -i "s|%OPTIMIZE_OPT%|${_optim}|g" qtbase/mkspecs/win32-g++/qmake.conf
  sed -i "s|%STATICFLAGS%||g" qtbase/mkspecs/win32-g++/qmake.conf # -static-libstdc++ -static-libgcc
  sed -i "s|-O2|${CXXFLAGS}|" qtbase/mkspecs/common/{g++,gcc}-base.conf
  sed -i "/^QMAKE_LFLAGS_RPATH/s| -Wl,-rpath,||g" qtbase/mkspecs/common/gcc-base-unix.conf
  sed -i "/^QMAKE_LFLAGS\s/s|+=|+= ${LDFLAGS}|g" qtbase/mkspecs/common/gcc-base.conf

  # Use python2 for Python 2.x
  #patch -p1 -i "${srcdir}"/use-python2.patch
  #sed -i -e "s|#![ ]*/usr/bin/python$|#!/usr/bin/python2|" \
  #  -e "s|#![ ]*/usr/bin/env python$|#!/usr/bin/env python2|" \
  #  $(find . -name '*.py')
}

build() {
  cd ${srcdir}
  rm -rf build-${CARCH}
  mkdir build-${CARCH} && cd build-${CARCH}
  #cd ${_realname}
  #touch qtbase/.gitignore
  #BUILD_WIN=$(pwd -W)
  #echo "BUILD_WIN $BUILD_WIN"

  #export QTDIR="${srcdir}"/${_realname}
  #export LD_LIBRARY_PATH="${QTDIR}"/qtbase/lib:"${QTDIR}"/qttools/lib:"${LD_LIBRARY_PATH}"
  #export QT_PLUGIN_PATH="${QTDIR}"/qtbase/plugins

  mkdir -p ${pkgdir}${MINGW_PREFIX}
  pushd ${pkgdir}${MINGW_PREFIX} > /dev/null
  QTDIR_WIN=$(pwd -W)
  echo "QTDIR_WIN $QTDIR_WIN"
  popd > /dev/null

  #pushd ${MINGW_PREFIX} > /dev/null
  #MINGW_WIN=$(pwd -W)
  #echo "MINGW_WIN $MINGW_WIN"
  #popd > /dev/null

  export INCLUDE="${MINGW_PREFIX}/${MINGW_CHOST}/include:${MINGW_PREFIX}/include:${MINGW_PREFIX}/include/mysql:${MINGW_PREFIX}/include/libxml2"
  export LIB="${MINGW_PREFIX}/${MINGW_CHOST}/lib:${MINGW_PREFIX}/lib"
  export CPATH="${MINGW_PREFIX}/${MINGW_CHOST}/include:${MINGW_PREFIX}/include:${MINGW_PREFIX}/include/mysql:${MINGW_PREFIX}/include/libxml2"
  export LIBRARY_PATH="${MINGW_PREFIX}/${MINGW_CHOST}/lib:${MINGW_PREFIX}/lib"
  export PATH=${srcdir}/${_realname}/qtbase/bin:${srcdir}/${_realname}/qtbase/lib:${PATH}

  ../${_realname}/configure.bat \
    -prefix ${QTDIR_WIN} \
    -opensource \
    -opengl desktop \
    -confirm-license \
    -debug-and-release \
    -plugin-sql-ibase \
    -plugin-sql-mysql \
    -plugin-sql-psql \
    -plugin-sql-odbc \
    -no-iconv \
    -icu \
    -fontconfig \
    -openssl \
    -no-dbus \
    -shared \
    -platform win32-g++ \
    -nomake tests \
    -nomake examples

  ${MINGW_PREFIX}/bin/mingw32-make ${MAKEFLAGS}

  # Fix paths
  #find "${QTDIR}" -name Makefile -exec sed -i "s|/usr/lib/qt/bin/qdoc|${QTDIR}/qtbase/bin/qdoc|g" {} +
  #find "${QTDIR}" -name Makefile.qmake-docs -exec sed -i "s|/usr/lib/qt/bin/qdoc|${QTDIR}/qtbase/bin/qdoc|g" {} +
  #find "${QTDIR}" -name Makefile -exec sed -i "s|/usr/lib/qt/bin/qhelpgenerator|${QTDIR}/qttools/bin/qhelpgenerator|g" {} +
  #find "${QTDIR}" -name Makefile.qmake-docs -exec sed -i "s|/usr/lib/qt/bin/qhelpgenerator|${QTDIR}/qttools/bin/qhelpgenerator|g" {} +
  #sed -i "s|/usr/lib/qt/bin/qhelpgenerator|${QTDIR}/qttools/bin/qhelpgenerator|g" qtwebkit/Source/Makefile.api
}

package() {

  cd build-${CARCH}

  export PATH=${pkgdir}${MINGW_PREFIX}/bin:${srcdir}/${_realname}/qtbase/bin:${srcdir}/${_realname}/qtbase/lib:${PATH}

  ${MINGW_PREFIX}/bin/mingw32-make ${MAKEFLAGS} install #INSTALL_ROOT="${pkgdir}"
  ${MINGW_PREFIX}/bin/mingw32-make ${MAKEFLAGS} docs
  ${MINGW_PREFIX}/bin/mingw32-make ${MAKEFLAGS} install_qch_docs

  install -D -m644 LGPL_EXCEPTION.txt \
  "${pkgdir}${MINGW_PREFIX}"/share/licenses/qt5/LGPL_EXCEPTION.txt

  # Remove dll's from lib
  rm -f "${pkgdir}${MINGW_PREFIX}/lib"/*.dll

  # Workaround for installing empty .pc files
  plain "---> Fix pkgconfig files..."
  local _pc_files=( $(find ${srcdir}/build-${CARCH} -type f -name Qt5*.pc) )
  cp -f ${_pc_files[@]} ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/

  # Fix wrong path in prl files
  #find "${pkgdir}${MINGW_PREFIX}/lib" -type f -name '*.prl' \
  #  -exec sed -i -e '/^QMAKE_PRL_BUILD_DIR/d;s/\(QMAKE_PRL_LIBS =\).*/\1/' {} \;

  # Fix wrong qmake path in pri file
  #sed -i "s|${srcdir}/${_realname}/qtbase|/usr|" \
  #  "${pkgdir}"/usr/lib/qt/mkspecs/modules/qt_lib_bootstrap_private.pri
}

md5sums=('SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         'SKIP'
         '10790f1672b5f2b83e5473ad6cd48cd3'
         '86899e8f2858c48d4827282c04696b76'
         'd675dbbe15d275c84f2792c75212ace0'
         '2488e70329d9bc71f8f4619ddfb8522a'
         '2235d876133956a788c393310fa8a526'
         '608a30dbf8f5c2146d7285b551b03252'
         '15bd4f83cf5a70377ea7e86c7505445b'
         'd9823799b2e960f0d204aa893056626a')
